package com.ocom.app.controller;


import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.ocom.app.feignclient.CompanyFeignClient;
import com.ocom.app.feignclient.PayConfigFeignClient;
import com.ocom.common.constant.CompanyServiceUrl;
import com.ocom.common.entity.card.EntityXfMerchantInfo;
import com.ocom.common.entity.company.EntityProductSuggestion;
import com.ocom.common.entity.master.EntityCompanyList;
import com.ocom.common.entity.master.EntityMngUserInfo;
import com.ocom.common.resp.Result;
import com.ocom.common.utils.ResultUtil;
import com.ocom.common.vo.company.GetMngUserInfoVo;
import com.ocom.security.authentication.YoCiUser;
import com.ocom.security.utils.SecurityUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/product_suggestion")
public class ProductSuggestionController {


  @Autowired
  private CompanyFeignClient companyFeignClient;

  @Autowired
  private PayConfigFeignClient payConfigFeignClient;

  /**
   * 3.2.1.建议列表(/appapi/product_suggestion/list)
   * @param page
   * @param size
   * @return
   */
  @GetMapping("/list")
  public Result list(@RequestParam("page") int page,
      @RequestParam("size") int size) {
    YoCiUser yoCiUser = SecurityUtils.getYoCiUser();
    return ResultUtil.success(
        companyFeignClient.productSuggestionList(yoCiUser.getComId().longValue(), yoCiUser.getId(),
            page, size).getData());
  }

  /**
   * 3.2.2.新的建议(/product_suggestions/create)
   * @param entityProductSuggestion
   * @return
   */
  @PostMapping("/create")
  public Result list(@RequestBody EntityProductSuggestion entityProductSuggestion) {
    YoCiUser yoCiUser = SecurityUtils.getYoCiUser();
    Long comId = yoCiUser.getComId();
    Long merchId = yoCiUser.getMerchId();
    GetMngUserInfoVo getMngUserInfoVo = new GetMngUserInfoVo();
    getMngUserInfoVo.setComId(comId);
    getMngUserInfoVo.setId(yoCiUser.getId());
    Result result = companyFeignClient.GetMngUserInfo(getMngUserInfoVo);
    Result companyListInfoOne = companyFeignClient.getCompanyListInfoOne(comId);
    Result getMerchInfo = payConfigFeignClient.GetMerchInfo(comId, merchId.intValue());
    EntityCompanyList entityCompanyList = JSON.parseObject(
        JSON.toJSONString(companyListInfoOne.getData()), EntityCompanyList.class);
    EntityXfMerchantInfo entityXfMerchantInfo = JSON.parseObject(
        JSON.toJSONString(getMerchInfo.getData()), EntityXfMerchantInfo.class);
    if (ObjectUtil.isNotNull(entityCompanyList)) {
      entityProductSuggestion.setComName(entityCompanyList.getComName());
    }
    if (ObjectUtil.isNotNull(entityXfMerchantInfo)) {
      entityProductSuggestion.setMchName(entityXfMerchantInfo.getMerchName());
    }
    entityProductSuggestion.setUserId(yoCiUser.getId());
    EntityMngUserInfo entityMngUserInfo = JSON.parseObject(JSON.toJSONString(result.getData()),
        EntityMngUserInfo.class);
    if (ObjectUtil.isNotNull(entityMngUserInfo)) {
      entityProductSuggestion.setUserName(entityMngUserInfo.getUserName());
    }
    companyFeignClient.productSuggestionCreate(comId, entityProductSuggestion);
    return ResultUtil.success();
  }

}
